﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using Microsoft.Xna.Framework;

namespace Ferpect.ComponentModel.Design
{
    public class ColorEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                Color colorValue = Color.Black;
                if (value is Color)
                {
                    colorValue = (Color)value;
                }
                ColorEditorDialog selectionControl = new ColorEditorDialog(colorValue, editorService);

                editorService.ShowDialog(selectionControl);

                value = selectionControl.Value;
            }

            return value;
        }
    }
}
